package time;

import java.math.BigInteger;

class Rational extends Number implements Comparable{
	public BigInteger getNumerator() {
		return numerator;
	}
	public void setNumerator(BigInteger numerator) {
		this.numerator = numerator;
	}
	public BigInteger getDenominator() {
		return denominator;
	}
	public void setDenominator(BigInteger denominator) {
		this.denominator = denominator;
	}

	private BigInteger numerator = new BigInteger("0");
	private BigInteger denominator = new BigInteger("1");
	public Rational() {
		// TODO Auto-generated constructor stub
		this(BigInteger.ZERO, BigInteger.ONE);
	}
	public Rational(BigInteger numerator, BigInteger denominator){
		BigInteger gcd = gcd(numerator, denominator);
		BigInteger b;
		if(denominator.compareTo(BigInteger.ZERO)>0){
			b=new BigInteger("1");
		}
		else{
			b= new BigInteger("-1");
		}
		this.numerator =  (numerator.multiply(b)).divide(gcd);
		this.denominator = denominator.abs().divide(gcd);
	}
	
	private static BigInteger gcd(BigInteger n, BigInteger d){
		BigInteger n1 = n.abs();
		BigInteger n2 = d.abs();
		BigInteger gcd = new BigInteger("1");
		for(BigInteger k = new BigInteger("1"); k.compareTo(n1)<=0 && k.compareTo(n2)<=0;k=k.add(BigInteger.ONE)){
			if(n1.mod(k).compareTo(BigInteger.ZERO)==0 && n2.mod(k).compareTo(BigInteger.ZERO)==0){
				gcd = k;
			}
		}
		
		return gcd;
	}
	
	public Rational add(Rational secondRational){
		BigInteger n = numerator.multiply(secondRational.getDenominator()).add((denominator)
				.multiply(secondRational.getNumerator()));
		BigInteger d = denominator.multiply(secondRational.getDenominator());
		
		return new Rational(n, d);
	}
	
	public Rational subtract(Rational secondRational){
		BigInteger n = numerator.multiply(secondRational.getDenominator()).subtract((denominator).multiply(secondRational.getNumerator()));
		BigInteger d = denominator.multiply(secondRational.getDenominator());
		
		return new Rational(n, d);
	}
	
	public Rational multiply(Rational secondRational){
		BigInteger n = numerator.multiply(secondRational.getNumerator());
		BigInteger d = denominator.multiply(secondRational.getDenominator());
		
		return new Rational(n, d);
	}
	
	public Rational devide(Rational secondRational){
		BigInteger n = numerator.multiply(secondRational.getDenominator());
		BigInteger d = denominator.multiply(secondRational.getNumerator());
		
		return new Rational(n, d);
	}
	
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		if(denominator.compareTo(BigInteger.ONE)==0){
			return numerator + "";
		}else{
			return numerator + "/" + denominator;
		}
	}
	
	@Override
	public boolean equals(Object obj) {
		if((this.subtract((Rational)obj).getNumerator().compareTo(BigInteger.ZERO)==0)){
			return true;
		}else{
			return false;
		}
	}
	@Override
	public int compareTo(Object o) {
		// TODO Auto-generated method stub
		if((this.subtract((Rational)o)).compareTo(BigInteger.ZERO)>0){
			return 1;
		}else if((this.subtract((Rational)o)).compareTo(BigInteger.ZERO)<0){
			return -1;
		}else
			return 0;
	}

	@Override
	public int intValue() {
		// TODO Auto-generated method stub
		return (int)doubleValue();
	}

	@Override
	public long longValue() {
		// TODO Auto-generated method stub
		return (long)doubleValue();
	}

	@Override
	public float floatValue() {
		// TODO Auto-generated method stub
		return (float)doubleValue();
	}

	@Override
	public double doubleValue() {
		// TODO Auto-generated method stub
		return numerator.divide(denominator).doubleValue();
	}
}